import json
import pymysql


# 函数名必须这样写 这是mitmdump规则
def response(flow):
    # 下面这个网址是通过fiddler获取到的 但是有些数据我们无法解密，所以需要用mitmdump捕获数据包然后做分析\

    if 'aweme.snssdk.com/aweme/v1/user/profile/other/?' in flow.request.url:
        result = json.loads(flow.response.text)['user']
        print(" ")
        print(" ")
        print(" ")
        print(" ")
        print(" ")
        print("******************************************************************************")
        print(" ")
        #print(result)

        user_info = {}

        #发布作品数
        if "dongtai_count" in result:
            user_info["dongtai_count"]=result["dongtai_count"]
        #粉丝数
        if "mplatform_followers_count" in result:
            user_info["mplatform_followers_count"]=result["mplatform_followers_count"]
        #获赞数
        if "total_favorited" in result:
            user_info["total_favorited"]=result["total_favorited"]
        #关注数
        if "following_count" in result:
            user_info["following_count"]=result["following_count"]
        #抖音号
        if "short_id" in result:
            user_info["short_id"]=result["short_id"]
        if "favoriting_count" in result:
            user_info["favoriting_count"]=result["favoriting_count"]
        #抖音名
        if "nickname" in result:
            user_info["nickname"] = result["nickname"]
        else:
            user_info["nickname"] = "伞梦"
        #个性签名
        if "signature" in result:
            user_info["signature"] = result["signature"].replace(' ', '').replace('\n', '').replace('\t', '')
        else:
            user_info["signature"] = "大家好。"
        #uid
        if "uid" in result:
            user_info["uid"] = result["uid"]
        else:
            user_info["uid"] = "-1"

        db = pymysql.connect(host="localhost", user="root", password="123456", database="dy", charset='utf8mb4')
        # 使用cursor()方法获取操作游标
        cursor = db.cursor()
        # SQL 插入语句
        sql = """INSERT INTO user_info(nickname,
                 uid, signature,dongtai_count,mplatform_followers_count,total_favorited,following_count,short_id,favoriting_count)
                 VALUES ('{0}', '{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}')""".format(user_info['nickname'], user_info['uid'], user_info['signature'],
                                                                                     user_info['dongtai_count'],user_info['mplatform_followers_count'],
                                                                                     user_info['total_favorited'],user_info['following_count'],user_info['short_id'],user_info['favoriting_count'])
        # user_info['nickname'], user_info['uid'],user_info['signature']
        try:
            # 执行sql语句
            cursor.execute(sql)
            print(sql)
            print("success")
            # 提交到数据库执行
            db.commit()
        except Exception as e:
            print("error")
            # Rollback in case there is any error
            print(e)
            db.rollback()
        # 关闭数据库连接
        db.close()

        # 有的用户修改了抖音号
        print(user_info)
        print(" ")
        print("******************************************************************************")
        print(" ")
        print(" ")
        print(" ")
        print(" ")
        print(" ")
